package bbs.model;

import java.util.*;

import org.apache.logging.log4j.*;

import bbs.db.StringList;

public class Entity extends HashMap<String, Object> {

	private static final long serialVersionUID = -4022788739623819282L;

	/**
	 * Log4j logger
	 */
	static final Logger logger = LogManager.getLogger(Entity.class);
	
	private StringList keyList = new StringList();

	public Entity() {
	}

	public Entity(int initialCapacity) {
		super(initialCapacity);
	}

	public Entity(Map<? extends String, ? extends Object> m) {
		super(m);
	}

	public Entity(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
	}
	
	public final String getEntityName() {
		return this.getClass().getSimpleName();
	}
	
	public final StringList getKeyList() {
		return this.keyList;
	}
	
	@Override
	public Object put( String key, Object value ) {
		if( ! keyList.contains( key ) ) {
			keyList.add( key );
		}
		return super.put(key, value);
	}
	
	public final boolean isInt( String key , int i ) {
		Number n = (Number) super.get(key);

		return n == null ? false : ( n.intValue() == i );
	}
	
	public final String getString( String key ) {
		return (String) super.get( key );
	}
	
	public final Integer getInteger( String key ) {
		Number n = (Number) super.get(key);
		if( n == null ) {
			return null;
		} else { 
			return n.intValue() ;
		}
	}
	
	public final int getInteger( String key, int defVal ) {
		Number n = (Number) super.get(key);
		if( n == null ) {
			return defVal ;
		} else { 
			return n.intValue() ;
		}
	}
	
	public final Double getDouble( String key ) {
		Number n = (Number) super.get(key);
		if( n == null ) {
			return null;
		} else { 
			return n.doubleValue();
		}
	}
	
	public final boolean isTrue( String key ) {
		Number n = (Number) super.get(key);
		if( n == null ) {
			return false;
		} else { 
			return n.intValue() > 0 ;
		}
	}
	
	@Override
	public final String toString() {
		String s = "";
		s += "[" + this.getEntityName() + "] ";
		Object v;
		for( String key : this.keyList ) {
			v = this.get( key );
			s += key + ": " + ( v == null ? "" : v ) + "; ";
		}
		return s;
	}

}
